<?php
/**
 * Обработчик события. Знает где и какие данные взять и как отрендерить.
 *
 * @package default
 * @author Alexander Sibiryakov
 **/
class DocumentEventHandler extends EventHandler 
{
	public $sm_db;
	
	private $oid;
	private $author_openid;
	private $create_dt;
	private $edited_dt;	
	private $url;
	private $title;
	private $announce;
	private $text;
	private $tags;
		
	// данные (ид итема, автор, дата добавления, урл, название, анонс, текст) -- каждое поле в отдельной ноде
	public function fetchData() {
		$sql = "SELECT * FROM store_objects WHERE store_objects.oid = ".$this->event->oid;
		$result = $this->sm_db->query($sql);
		if ($result) {
			$row = mysql_fetch_assoc($result);
			mysql_free_result($result);
			
			// выберем теги
			$sql = "SELECT name FROM store_class_tag 
						INNER JOIN store_tag_subject ts ON ts.tag_oid = store_class_tag.oid
						WHERE ts.subject_oid = ".$this->event->oid;
			$result = $this->sm_db->query($sql);
			while ($tag = mysql_fetch_assoc($result)) {
				$this->tags[] = $tag['name'];
			}
			
			$this->mapTypicalData($row);
			$this->mapData($row);
		}
	}
	
	protected function mapTypicalData($data) {
		$hash = unserialize($data['data']);
		
		$this->oid = $this->event->oid;
		$this->author_openid = $this->event->openid;
		$this->create_dt =date($this->date_format, RL_CyrDate::newFromStr("%Y-%m-%d %H:%i:%s", $data['ctime'])->unix_mark());
		$this->edited_dt = date($this->date_format, RL_CyrDate::newFromStr("%Y-%m-%d %H:%i:%s", $data['mtime'])->unix_mark());			
		$this->url = $this->url_gen->createItemGen()->genUrl(array($hash['community_oid'], $this->event->oid));		
	}
	
	protected function mapData($data) {
		$hash = unserialize($data['data']);
		$this->title = $hash['title'];
		$this->announce = $hash['announce'];
		$this->text = $hash['body'];
	}
	
	public function createElements(DOMDocument $dom ) {

		$chlds = array();
		$chlds[] = $dom->createElement("id", $this->oid);
		$chlds[] = $dom->createElement("author_openid_url", $this->esc($this->author_openid));
		$chlds[] = $dom->createElement("create_datetime", $this->esc($this->create_dt));
		$chlds[] = $dom->createElement("edit_datetime", $this->esc($this->edited_dt));		
		$chlds[] = $dom->createElement("url", $this->esc($this->url));
		
		if (count($this->tags)>0) {
			$keywords_elt = $dom->createElement("keywords");
			foreach ($this->tags as $name) {
				$keyword_elt = $dom->createElement("keyword", $this->esc($name));
				$keywords_elt->appendChild($keyword_elt);
			}
			
			$chlds[] =$keywords_elt; 
		}
		foreach($this->createCustomElements($dom) as $elt) $chlds[] = $elt;
		return $chlds;
	}
	
	protected function createCustomElements($dom) {
		$chlds = array();
		$chlds[] = $dom->createElement("title", $this->esc($this->title));
		$chlds[] = $dom->createElement("summary", $this->esc($this->announce));
		$chlds[] = $dom->createElement("text", $this->esc($this->text));
		$chlds[] = $dom->createElement("is_comment", 0);
		return $chlds;
	}
	
} // END class 